
import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/framework/bean/x_bean_menu.dart';
import 'package:xsdk_flutter/xsdk/framework/page/x_page_state.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_image.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';


/*
内置BottomNavigatorBar (底部导航栏,图标方式显示)
TabBar和BottomNavigationBar一次只能使用一个
 */
abstract class XPageBottomNavigationBarState extends XPageState {

  //初始时同时加载所有页面,使用IndexedStack
  bool isLoadAllPages=false;

  XPageBottomNavigationBarState(
      {
        super.isKeepPageState=false,
        super.xTag,
        this.isLoadAllPages=false,//初始时同时加载所有页面,使用IndexedStack
      }
      );


  @required
  Widget getBottomNavigationBarViewWidget(XBeanMenuItem menuItem);


  //更新BottomNavigationBar数据
  void updateXBottomNavigationBar(
      {
        List<XBeanMenuItem>? items,
        TextStyle? selectTextStyle,
        TextStyle? unSelectTextStyle,
        Color? bottomNavigationBarBackgroundColor,
      }
      ){

    setState(() {
      widget.currentBottomNavBarIndex=0;

      _bottomNavigationBarSelectTextStyle=selectTextStyle;
      _bottomNavigationBarUnSelectTextStyle=unSelectTextStyle;
      _bottomNavigationBarUnSelectTextStyle=unSelectTextStyle;
      _bottomNavigationBarBackgroundColor = bottomNavigationBarBackgroundColor;

      //
      _listBottomNavigationItems.clear();
      if(items!=null){
        _listBottomNavigationItems.addAll(items);
      }
    });

  }




  //重写/////////////////////////////////////////////////////

  //创建内置BottomNavigationBar
  @override
  Widget? createBottomNavigationBar(){

    if(_listBottomNavigationItems.isEmpty){
      return XWidgetUtils.getLayoutContainer();
    }

    return BottomNavigationBar(
      selectedLabelStyle: _bottomNavigationBarSelectTextStyle,
      unselectedLabelStyle: _bottomNavigationBarUnSelectTextStyle,
      items:_listBottomNavigationItems.map((XBeanMenuItem tabItemData) {
        return BottomNavigationBarItem(
          label: tabItemData.text??'',//不能为空,否则报错
          icon: XImageUtils.getImageWidgetWithAsset(
            tabItemData.image!,
            color: _bottomNavigationBarUnSelectTextStyle?.color,
            width: tabItemData.imageWidth,
            height: tabItemData.imageHeight,
          ),
          activeIcon:XImageUtils.getImageWidgetWithAsset(
            tabItemData.selectImage==null?tabItemData.image!:tabItemData.selectImage!,
            color: _bottomNavigationBarSelectTextStyle?.color,
            width: tabItemData.imageWidth,
            height: tabItemData.imageHeight,
          ),

        );
      }).toList(),
      //点击监听
      onTap: (int index){
        //通知更新state,切换界面,下标从0开始
        //XLogUtils.printLog('_bottomNavigationBarIndex=$index');
        if(widget.currentBottomNavBarIndex!=index){
          setState(() {
            //更新
            widget.currentBottomNavBarIndex=index;

            //切换页面,PageView方式(IndexedStack不需要)
            if(!isLoadAllPages){
              //没有动画效果
              //_bottomNavigationBarPageController?.jumpToPage(index);

              //使用动画效果
              _bottomNavigationBarPageController?.animateToPage(index,
                  duration: const Duration(milliseconds: 200), curve: Curves.ease);
            }

          });
        }
      },
      /*
      类型:
      shifting: 默认值,背景白色(设置背景色无效),只显示当前item的lable,其他不显示,鼠标悬停会弹出label提示
      fixed: 设置背景色生效,所有item都显示label
       */
      type: BottomNavigationBarType.fixed,
      //背景色
      backgroundColor: _bottomNavigationBarBackgroundColor??Colors.white,
      //当前位置下标值
      currentIndex: widget.currentBottomNavBarIndex,
      //当前字体颜色
      //fixedColor: _bottomNavigationBarCurrentTextColor,//同selectedItemColor??
      selectedItemColor: _bottomNavigationBarSelectTextStyle?.color,
      unselectedItemColor: _bottomNavigationBarUnSelectTextStyle?.color,

    );
  }


  //创建BottomNavigationBarView
  @override
  Widget createBottomNavigationBarView(){

    if(_listBottomNavigationItems.isEmpty){
      return XWidgetUtils.getLayoutContainer();
    }

    if(isLoadAllPages){
      //IndexedStack,初始化所有页面,不需要设置super.isKeepPageState,自动缓存所有页面的状态
      //IndexedStack没有过渡动画
      return IndexedStack(
        index: widget.currentBottomNavBarIndex,
        children: _listBottomNavigationItems.map((XBeanMenuItem menuItem) {
          return getBottomNavigationBarViewWidget(menuItem);
        }).toList(),
      );

    }else{

      _bottomNavigationBarPageController?.dispose();
      _bottomNavigationBarPageController = PageController(initialPage: widget.currentBottomNavBarIndex);

      //只初始当前页面的索引页,后期点击每个item显示时再加载,页面缓存状态需要设置super.isKeepPageState=true
      // return PageView(
      //   ///禁止左右滑动
      //   physics: const NeverScrollableScrollPhysics(),
      //   controller: _bottomNavigationBarPageController,
      //   children: _listBottomNavigationItems.map((XBeanMenuItem menuItem) {
      //     return getBodyWidget(menuItem: menuItem);
      //   }).toList(),
      // );
      //PageView有过渡动画,实测使用此切换动画更流畅
      return PageView.builder(
        //禁止左右滑动
        physics: const NeverScrollableScrollPhysics(),
        //允许左右滑动时,监听滑动切换,更新bottomNavigationBar的按钮当前位置
        // onPageChanged: (index){
        //   setState(() {
        //     if (_bottomNavigationBarIndex != index) {
        //       _bottomNavigationBarIndex = index;
        //     }
        //   });
        // },
        itemBuilder: (context, index) {
          return getBottomNavigationBarViewWidget(_listBottomNavigationItems[index]);
        },
        itemCount: _listBottomNavigationItems.length,
        controller: _bottomNavigationBarPageController,
      );

    }

  }

  //////////////////////////////////////////////////////////////////////////

  //背景颜色
  Color? _bottomNavigationBarBackgroundColor;
  final List<XBeanMenuItem> _listBottomNavigationItems=[];//注意:item不能为null

  PageController? _bottomNavigationBarPageController;//PageView使用

  TextStyle? _bottomNavigationBarSelectTextStyle;
  TextStyle? _bottomNavigationBarUnSelectTextStyle;

  @override
  void dispose() {
    //手动释放bottomNavigationBar的PageController
    _bottomNavigationBarPageController?.dispose();
    //
    super.dispose();
  }


  /////////////////////////////////////////////////////

  //这里页面不会使用getBodyWidget(),重写消耗
  @override
  Widget getBodyWidget(BuildContext context) {
    return XWidgetUtils.getLayoutContainer();
  }

}